<?php

class ListingAttribute_AdminController extends Ac_AdminController
{
    public function deleteMakeAction()
    {
        //Remove Models from this make
        $listing = Model_Listing_Attribute::remove(array('path' => $_GET['id']));
        
        //Remove the make
        $listing = Model_Listing_Attribute::remove(array('_id' => new MongoId($_GET['id'])));
        
        $this->_redirect($this->view->geturl('list','listingAttribute', null, 'admin1'));
    }

    public function deleteModelAction()
    {      
        //Remove the model
        $listing = Model_Listing_Attribute::remove(array('_id' => new MongoId($_GET['id'])));
        
        $this->_redirect($this->view->geturl('display','listingAttribute', null, 'admin1', array('id'=> $_GET['idmake'])));
    }

    public function addmakeAction()
    {
        if ((!empty($_GET['name_en']) || !empty($_GET['name_tc']) || !empty($_GET['name_sc'])) && !empty($_GET['type'])){
            $cat = new Model_Listing_Attribute();
            $cat->nm_en = $_GET['name_en'];
            $cat->nm_tc = $_GET['name_tc'];
            $cat->nm_sc = $_GET['name_sc'];
            $cat->type = $_GET['type'];

            $cat->save();            
        }
        $this->_redirect($this->view->geturl('list','listingAttribute', null, 'admin1'));
    }

    public function addmodelAction()
    {
        
        if (!empty($_GET['name_en']) || !empty($_GET['name_tc']) || !empty($_GET['name_sc'])){
            $cat = new Model_Listing_Attribute();
            $cat->nm_en = $_GET['name_en'];
            $cat->nm_tc = $_GET['name_tc'];
            $cat->nm_sc = $_GET['name_sc'];
            $cat->py    = $_GET['pinyin'];
            $cat->order = (int)$_GET['order'];
            $cat->save();
            $cat->path = array(new MongoId($_GET['id']), $cat->_id);
            $cat->save();
        }
        $this->_redirect($this->view->geturl('display','listingAttribute', null, 'admin1', array('id'=> $_GET['id'])));
        
    }

    public function dashboardAction()
    {
        $this->view->num_active_attribute = Model_Listing_Attribute::all()->count();
    }

    public function menuAction()
    {
        $this->view->index = $this->_getParam('index');
    }

    public function indexAction() 
    {
        $this->listAction();
    }
    
    public function listAction() 
    {
        $per_page = 20;
        $page      = !empty($_GET['page']) ? $_GET['page'] : 1;
        $this->view->per_page = $per_page;
        $this->view->listing_number = Model_Listing_Attribute::all(array('path' => array('$exists' => 0)))->count();
        $this->view->page = $page;

        if ($page>1) {
            $skip_page = ($_GET['page']-1) * $per_page;
        }

        if ((!empty($_GET['attribute'])) && (!empty($_GET['type']))) {
            $term = new MongoRegex('/'.$_GET['attribute'].'/i');
            if ($page>1) {
                $this->view->list           = Model_Listing_Attribute::all(array('path' => array('$exists' => 0), 'nm_en' => $term, 'type' => $_GET['type']))->skip($skip_page)->limit($per_page)->sort(array('nm_en'=>1));
            }
            else {
                $this->view->list           = Model_Listing_Attribute::all(array('path' => array('$exists' => 0), 'nm_en' => $term, 'type' => $_GET['type']))->limit($per_page)->sort(array('nm_en'=>1));
            }
            $this->view->listing_number = $this->view->list->count();            
        }
        elseif ((!empty($_GET['attribute'])) && (empty($_GET['type']))) {
            $term = new MongoRegex('/'.$_GET['attribute'].'/i');
            if ($page>1) {
                $this->view->list           = Model_Listing_Attribute::all(array('path' => array('$exists' => 0), 'nm_en' => $term))->skip($skip_page)->limit($per_page)->sort(array('nm_en'=>1));
            }
            else {
                $this->view->list           = Model_Listing_Attribute::all(array('path' => array('$exists' => 0), 'nm_en' => $term))->limit($per_page)->sort(array('nm_en'=>1));
            }
            $this->view->listing_number = $this->view->list->count();
        }
        elseif ((empty($_GET['attribute'])) && (!empty($_GET['type']))) {
            $term = new MongoRegex('/'.$_GET['attribute'].'/i');
            if ($page>1) {
                $this->view->list           = Model_Listing_Attribute::all(array('path' => array('$exists' => 0), 'type' => $_GET['type']))->skip($skip_page)->limit($per_page)->sort(array('nm_en'=>1));
            }
            else {
                $this->view->list           = Model_Listing_Attribute::all(array('path' => array('$exists' => 0), 'type' => $_GET['type']))->limit($per_page)->sort(array('nm_en'=>1));
            }
            $this->view->listing_number = $this->view->list->count();
        }
        else {
            if ($page>1) {
                $this->view->list           = Model_Listing_Attribute::all(array('path' => array('$exists' => 0)))->sort(array('or' => 1, 'nm_en' => 1))->skip(($_GET['page']-1) * $per_page)->limit($per_page)->sort(array('nm_en'=>1));
            }
            else {
                $this->view->list           = Model_Listing_Attribute::all(array('path' => array('$exists' => 0)))->sort(array('or' => 1, 'nm_en' => 1))->limit($per_page)->sort(array('nm_en'=>1));
            }
            $this->view->listing_number = $this->view->list->count();
        }

        $this->view->type = Model_Listing_Attribute::getTypes();

        $this->render('list');  

    }
  
    public function displayAction()
    {
        $attribute = Model_Listing_Attribute::find($_GET['id']);

        $this->view->attribute = $attribute;
        $this->render('display');
    }

    private function processRefineSearchForm($page, $per_page, $sort, &$query, &$fields, &$additionalParameters)
    {
        $results = false;

        $values     = array();
        $valuesplus = array();
        $get     = $_GET;

        if (!empty($_GET['keyword'])) {
            $values['firstname']      = strtolower(trim($_GET['keyword']));
            $values['lastname']       = strtolower(trim($_GET['keyword']));
            $values['username']       = strtolower(trim($_GET['keyword']));
        }

        foreach($values as $k => $val) {
          $values[$k] = ' '.$k.':'.$val.' ';
        }

        if (!empty($values)) {
            $query = implode(' ', $values);
        }

        if (empty($query)) {
            $query = '*:*';
        }

        switch ($_GET['sort']) {
            case 'rel':
                $sort = 'score desc';
                break;
            case 'recent':
                $sort = 'posted_at desc';
                break;
            case 'status':
                $sort = 'status desc';
                break;
            case 'email':
                $sort = 'email asc';
                break;
            default:
                $_GET['sort'] = 'recent';
                $sort         = 'posted_at desc';
        }

        $additionalParameters = array(
            'fl'   => '*,score',
            'sort' => $sort,
        );

        $fq                     = '';
        $valuesplus             = array();
        $valuesplus[] = ' +type_obj:'.Model_Company::TYPE;


        if (!empty($valuesplus)) {
            $fq .= implode(' ', $valuesplus);
        }

        if (!empty($fq)) {
            $additionalParameters['fq'] = $fq;
        }
    }
    
    public function editAction()
    {
        if (!empty($_POST['id'])) {
            $to_update = array();
            $key       = $_POST['id'];
            $value_en  = $_POST['name_en'];
            $value_tc  = $_POST['name_tc'];
            $value_sc  = $_POST['name_sc'];
            $pinyin    = $_POST['pinyin'];
            $order     = (int)$_POST['order'];
            $arr = explode(',',$_POST['tags']);
            array_walk($arr, function(&$item, $key) {trim($item);});
            $to_update = array('$set' => array('nm_en' => $value_en, 'nm_tc' => $value_tc, 'nm_sc' => $value_sc, 'py' => $pinyin, 'or' => $order));
            if(!empty($arr)) {
                $to_update['$set']['tg'] = $arr;
            } elseif(!empty($to_update['$set']['tg'])) {
                unset($to_update['$set']['tg']);
            }
            Model_Listing_Attribute::update(array('_id'=>new MongoId($key)), $to_update, array('multiple' => false, 'upsert' => false, 'safe'=>true));
        }
//        Model_Translate::addTranslation($key, $value, $locale);
        echo 'ok';exit;
    }    
}
